import { AttributeViewInfo, BlockViewInfo, IObjectAttributeView, IObjectBlockView, IObjectView, objectview, OBVComponent } from '@feng3d/objectview';
import GUI from 'lil-gui';

/**
 * 默认对象属性块界面
 */
@OBVComponent('OBVDefault')
export class OBVDefault implements IObjectBlockView
{
    private _space: Object;
    private _blockName: string;

    private attributeViews: IObjectAttributeView[];
    private itemList: AttributeViewInfo[];

    objectView: IObjectView;

    constructor(blockViewInfo: BlockViewInfo)
    {
        this._space = blockViewInfo.owner;
        this._blockName = blockViewInfo.name;
        this.itemList = blockViewInfo.itemList;
    }

    initview(gui: GUI): void
    {
        if (this._blockName && this._blockName.length > 0)
        {
            gui.addFolder(this._blockName);
        }

        this.attributeViews = [];
        const objectAttributeInfos = this.itemList;
        for (let i = 0; i < objectAttributeInfos.length; i++)
        {
            const displayObject = objectview.getAttributeView(objectAttributeInfos[i]);
            displayObject.objectView = this.objectView;
            displayObject.objectBlockView = this;
            displayObject.initview(gui);
            this.attributeViews.push(<any>displayObject);
        }
    }

    dispose()
    {
        for (let i = 0; i < this.attributeViews.length; i++)
        {
            const displayObject = this.attributeViews[i];
            displayObject.objectView = null;
            displayObject.objectBlockView = null;
        }
        this.attributeViews.length = 0;
    }

    get space(): Object
    {
        return this._space;
    }

    set space(value: Object)
    {
        this._space = value;
        for (let i = 0; i < this.attributeViews.length; i++)
        {
            this.attributeViews[i].space = this._space;
        }
    }

    get blockName(): string
    {
        return this._blockName;
    }

    updateView(): void
    {
        for (let i = 0; i < this.attributeViews.length; i++)
        {
            this.attributeViews[i].updateView();
        }
    }

    getAttributeView(attributeName: String)
    {
        for (let i = 0; i < this.attributeViews.length; i++)
        {
            if (this.attributeViews[i].attributeName === attributeName)
            {
                return this.attributeViews[i];
            }
        }

        return null;
    }
}
